const mongoose = require('mongoose');
const { toJSON, paginate } = require('./plugins');

const states = [
    'Unassigned', // 待领取
    'Assigned', // 执行中
    'Completed', // 已完成
];

const taskSchema = mongoose.Schema(
    {
        title: {
            type: String,
            required: true,
            trim: true,
        }, // 工单标题
        status: {
            type: String,
            enum: states,
            default: 'InPublic',
        }, // 工单状态
        description: {
            type: String,
            default: '',
        },
        requestedBy: {
            type: mongoose.SchemaTypes.ObjectId,
            ref: 'User',
            required: true,
        },
    },
    {
        timestamps: true,
    }
);

// add plugin that converts mongoose to json
taskSchema.plugin(toJSON);
taskSchema.plugin(paginate);

/**
 * @typedef Task
 */
const Task = mongoose.model('Task', taskSchema);

module.exports = Task;